package ctl

import "Todolist/pkg/e"

// 最基础的返回resp
type Response struct {
	Status int         `json:"status"`
	Data   interface{} `json:"data"`
	Msg    string      `json:"msg"`
	Error  string      `json:"error"`
}

// 带数组形式的返回
type DataList struct {
	Item  interface{} `json:"item"`
	Total int64       `json:"total"`
}

// 用户登录专属的，带用户信息和token的返回resp

// 带有追踪信息的错误返回
type TrackedErrorResponse struct {
	Response
	TraceId string `json:"trace_id"`
}

func RespList(items interface{}, total int64) Response {
	return Response{
		Status: 200,
		Data: DataList{
			Item:  items,
			Total: total,
		},
		Msg: "ok",
	}
}

// 正常的返回成功的函数
func RespSuccess(code ...int) *Response {
	status := e.Success
	if code != nil {
		status = code[0]
	}
	return &Response{
		Status: status,
		Data:   "操作成功",
		Msg:    e.GetMsg(status),
	}

}

func RespSuccessWithData(data interface{}, code ...int) *Response {
	status := e.Success
	if code != nil {
		status = code[0]
	}
	return &Response{
		Status: status,
		Data:   data,
		Msg:    e.GetMsg(status),
	}

}

func RespError(err error, data interface{}, code ...int) *TrackedErrorResponse {
	status := e.Success
	if code != nil {
		status = code[0]
	}
	return &TrackedErrorResponse{
		Response: Response{
			Status: status,
			Data:   data,
			Msg:    e.GetMsg(status),
			Error:  err.Error(),
		},
		TraceId: "", //todo
	}

}
